<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model {

	  public function __construct()
	    {
	        parent::__construct();
	  
	    }
	  /**
	  *查询key
	  */
    public function getKey($username){
    	

      $this->db->where('username',$username);  
      $this->db->select('key'); 
      $query= $this->db->get('file_user'); 
      return $query->result();
    }
  public  function updateKey($id,$newkey){ 
  
    	 $data=array(
                   	'key'=>$newkey, 
                    );

         $this->db->where('id',$id);  

        return $this->db->update("file_user",$data);
    }

   public function checkPass($username,$password){
    	 $password=md5($password);
    	 $sql = "SELECT * FROM file_user WHERE username = ? AND password = ? "; 	
		    $query = $this->db->query($sql, array($username, $password));
    	 return $query->result();
    }
    /**
    *添加用户
    */
    public function addUser($username,$password,$level,$key){

        $sql = "SELECT * FROM file_user WHERE username = ? ";  
        $query = $this->db->query($sql, array($username));
        $res =  $query->result();
        $id = $res[0]->id;
        if($id != ''){
         return '110';
          exit;
       }
      $data = array(

               'username' => $username ,

               'password' => $password ,

               'autherity' => $level,

               'key' => $key,

               'mercode' => $username,

            );
          return  $this->db->insert(' file_user', $data); 
    }
	/**
	*查询指定时间段内的流量
	*/
	public  function getDaydata($start,$end,$mercode){
		$sql = "SELECT dayflow,date FROM file_dayflow WHERE  date >= '$start'  and date < '$end 23:59:59' and mercode = '$mercode'";  
		
        $query = $this->db->query($sql);
        $res =  $query->result();
		return $res;
	}
	/*
	*查询本月每天数据
	*/
	public function getTheday($mercode){
		$start= date('Y-m-1');
		$sql = "SELECT dayflow ,date FROM file_dayflow WHERE  mercode = '$mercode' and  date>'$start 00:00:00'   ";  
	
        $query = $this->db->query($sql);
		
        $res =  $query->result();
		
		return $res;
	}
	/**
	*每天流量存储
	*@$date日期
	*@$mercode商户号
	*@$dayflow当日流量
	*/
	public function dayflow($mercode,$dayflow,$date){
		$sql = "SELECT * FROM file_dayflow WHERE mercode='$mercode' date = '$date 00:00:00' "; 
		
        $query = $this->db->query($sql);
        $res =  $query->result();
		
        $id = $res[0]->id;
		if($id){
			 $data=array(
                   	'dayflow'=>$dayflow, 
                    );

			$this->db->where('id',$id);  

			$res =  $this->db->update("file_dayflow",$data);
		}else{
			$data=array(
		   'mercode'=>$mercode,
		   'dayflow'=>$dayflow,
		   'date'=>$date
			); 
			$res = $this->db->insert("file_dayflow",$data);
		}
	
		
		return $res;
	}
	
}